package com.ry.framework.mongo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ry.framework.mongo.bean.Topic;
import com.ry.framework.mongo.service.MongoService;

@Controller
@RequestMapping(value = "/mongo")
public class MongoController {

	// 获得SpringBoot提供的mongodb的GridFS对象,上传下载所用
    @Autowired
    private GridFsTemplate gridFsTemplate;
	
	@Autowired
	private MongoService mongoService;
	
	@RequestMapping("/findOne")
	@ResponseBody
	public String findOne() {
		Topic t = mongoService.findOne();
		System.out.println(JSONObject.toJSONString(t));
		return JSON.toJSONString(t);
	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	public String findAll() {
		List<Topic> list = mongoService.findAll();
		System.out.println(JSONObject.toJSONString(list));
		return JSON.toJSONString(list);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save() {
		Topic t = new Topic();
		t.setEmpid(30);
		t.setEname("save测试");
		t.setDev("savedev");
		String result = mongoService.save(t);
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update() {
		Topic t = new Topic();
		t.setEmpid(10);
		String result = mongoService.update(t);
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete() {
		Topic t = new Topic();
		String result = mongoService.delete();
		return result;
	}
	
}
